﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DisplayBoardInfo.Network;

namespace TestClientGui
{
    public partial class Form1 : Form
    {
        private static DisplayBoardInfo.Network.Client c;
        private DisplayBoardInfo.Network.Messages.Content.User loggedUser;

        public Form1()
        {
            InitializeComponent();

            c = new DisplayBoardInfo.Network.Client();
            c.MessageRecived += new Client.MessageRecivedHandler(c_MessageRecived);

            dateTimePicker1.Value = DateTime.Now;
            DisplayBoardInfo.Logger.Logger.Logged += new DisplayBoardInfo.Logger.Logger.LoggedHandler(Logger_Logged);
        }

        void Logger_Logged(object sender, DisplayBoardInfo.Logger.LoggerEventArgs args)
        {
            textBox1.Text += args.LogMessage.ToString();
            textBox1.Text += Environment.NewLine;
        }

        void c_MessageRecived(object sender, MessageEventArgs args)
        {
            if (args.Message.Type == DisplayBoardInfo.Network.Messages.NetworkMessageTypes.Hello)
            {
                Cursor = Cursors.Arrow;
                this.Enabled = true;
                loggedUser = (DisplayBoardInfo.Network.Messages.Content.User)args.Message.Content;
                MessageBox.Show("Zalogowany");
            }
            else if (args.Message.Type == DisplayBoardInfo.Network.Messages.NetworkMessageTypes.Appointments)
            {
                listBox1.Items.Clear();
                IEnumerable<DisplayBoardInfo.Network.Messages.Content.Appointment> result = (IEnumerable<DisplayBoardInfo.Network.Messages.Content.Appointment>)args.Message.Content;
                foreach (DisplayBoardInfo.Network.Messages.Content.Appointment item in result)
                {
                    //listBox1.Items.Add(String.Format("Pacjent: {0} {1}; Doktor: {2} {3}; Godzina: {4}", item.Patient.Name, item.Patient.LastName, item.Doctor.Name, item.Doctor.Lastname, item.Date.ToShortDateString()));
                    listBox1.Items.Add(item);
                }
            }
            else
            {
                Console.WriteLine(args.Message.Content.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)//zaloguj
        {
            Cursor = Cursors.WaitCursor;
            //this.Enabled = false;
            //c.Connect("127.0.0.1");
            c.Connect("192.168.1.102");

            DisplayBoardInfo.Network.Messages.Content.Hello hello = new DisplayBoardInfo.Network.Messages.Content.Hello();
            hello.Username = "lekarz2";
            hello.Password = "12345";
            c.Send(new DisplayBoardInfo.Network.Messages.NetworkMessage(DisplayBoardInfo.Network.Messages.NetworkMessageTypes.Hello, hello));
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                switch (comboBox1.SelectedItem.ToString())
                {
                    case "Start":
                        c.Send(new DisplayBoardInfo.Network.Messages.NetworkMessage(DisplayBoardInfo.Network.Messages.NetworkMessageTypes.Session_Start, null));
                        break;
                    case "Przerwa":
                        c.Send(new DisplayBoardInfo.Network.Messages.NetworkMessage(DisplayBoardInfo.Network.Messages.NetworkMessageTypes.Session_Pause, null));
                        break;
                    case "Koniec":
                        c.Send(new DisplayBoardInfo.Network.Messages.NetworkMessage(DisplayBoardInfo.Network.Messages.NetworkMessageTypes.Session_End, null));
                        break;
                }
            }
            catch (ClientException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DisplayBoardInfo.Network.Messages.Content.AppointmentArgs args = new DisplayBoardInfo.Network.Messages.Content.AppointmentArgs();
            args.ConnectedWithDoctor = "lekarz1";
            args.Date = dateTimePicker1.Value;
            DisplayBoardInfo.Network.Messages.NetworkMessage m = new DisplayBoardInfo.Network.Messages.NetworkMessage(DisplayBoardInfo.Network.Messages.NetworkMessageTypes.GetAppointments, args);
            c.Send(m);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            List<DisplayBoardInfo.Network.Messages.Content.Appointment> appo = new List<DisplayBoardInfo.Network.Messages.Content.Appointment>();
            foreach (object item in listBox1.Items)
            {
                appo.Add((DisplayBoardInfo.Network.Messages.Content.Appointment)item);
            }
            DisplayBoardInfo.Network.Messages.NetworkMessage m = new DisplayBoardInfo.Network.Messages.NetworkMessage(DisplayBoardInfo.Network.Messages.NetworkMessageTypes.Session_ChangeList, appo);
            c.Send(m);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            listBox1.Items.Remove(listBox1.SelectedItem);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            List<DisplayBoardInfo.Network.Messages.Content.Appointment> appo = new List<DisplayBoardInfo.Network.Messages.Content.Appointment>();
            foreach (object item in listBox1.Items)
            {
                appo.Add((DisplayBoardInfo.Network.Messages.Content.Appointment)item);
            }

            DisplayBoardInfo.Network.Messages.NetworkMessage m2 = new DisplayBoardInfo.Network.Messages.NetworkMessage(DisplayBoardInfo.Network.Messages.NetworkMessageTypes.Session_NextPatient, appo);
            c.Send(m2);

            listBox1.Items.Remove(listBox1.Items[0]);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<DisplayBoardInfo.Network.Messages.Content.Appointment> al = new List<DisplayBoardInfo.Network.Messages.Content.Appointment>();
            DisplayBoardInfo.Network.Messages.Content.Appointment a = new DisplayBoardInfo.Network.Messages.Content.Appointment();
            a.Date = DateTime.Now;


            a.Doctor = loggedUser;

            a.ExpectedTime = 17;
            a.ID = -1;
            a.Number = 1;

            DisplayBoardInfo.Network.Messages.Content.Patient p = new DisplayBoardInfo.Network.Messages.Content.Patient();
            p.ID = -1;
            p.LastName = "Bolowski";
            p.Name = "Kazimierz";
            p.PESEL = "909890809";
            a.Patient = p;

            al.Add(a);

            DisplayBoardInfo.Network.Messages.NetworkMessage m2 = new DisplayBoardInfo.Network.Messages.NetworkMessage(DisplayBoardInfo.Network.Messages.NetworkMessageTypes.AddAppointment, al);
            c.Send(m2);
        }
    }
}
